//1.交换2个数

let a = 10;
let b = 20;

//算法1：借助第三个变量当桥梁

// let temp = a;
// a = b;
// b = temp;

//算法2:不借助第三个变量，只通过a,b两个数相加，再做差
a = a + b; //a=30
b = a - b; // b=10
a = a - b; //a=20

//2.数组去重
const arr = [2, 3, 4, 54, 12, 12, 12, 23, 23, 12, 32];

//算法1：new Set()
const result = [...new Set(arr)];
//console.log(result);

//算法2: 遍历+indexOf,遍历+includes
let tempArr = [];
// arr.forEach((item) => {
//   if (tempArr.indexOf(item) === -1) {
//     tempArr.push(item);
//   }
// });

arr.forEach((item) => {
  if (!tempArr.includes(item)) {
    tempArr.push(item);
  }
});

//3.检测字符串中有多少个重复字母，统计出来
const str = "hello welcome to china";

//需求：统计出字符串重复的字母次数，返回一个对象,例如：{ h:2,e:3,l:3,o:3,.... }
const obj = {};

for (let i = 0; i < str.length; i++) {
  if (str[i] in obj) {
    obj[str[i]]++;
  } else {
    obj[str[i]] = 1;
  }
}

//4. 数组扁平化
const arr1 = [23, 345, [253, [35, 23, 2, [232, 234]], 234], 243, 24];

//算法1：使用数组的flat方法
let result1 = arr1.flat(3);
//console.log("result:", result1);
//算法2：递归处理,遍历数组的每个元素，如果是数组继续递归，如果不是则push到一个新数组中
function flatten(arr) {
  //如果不是数组，直接返回
  if (!Array.isArray(arr)) return;
  //深拷贝
  const cloneArr = JSON.parse(JSON.stringify(arr));
  //创建一个存放扁平化后的新数组
  let newArr = [];
  //遍历数组每个元素
  cloneArr.forEach((item) => {
    if (Array.isArray(item)) {
      newArr = newArr.concat(flatten(item));
    } else {
      newArr.push(item);
    }
  });

  return newArr;
}
// console.log("通过递归扁之前数组：", arr1);
// console.log("通过递归扁平化的结果：", flatten(arr1));

//5.斐波那契数列
/**
 *
 * @param {number} n 第几位的斐波那契数列值
 */
function fibonacci(n) {
  if (n === 1 || n === 2) {
    return 1;
  }

  return fibonacci(n - 1) + fibonacci(n - 2);
}

for (let i = 1; i <= 10; i++) {
  //console.log("斐波那契数列结果：", fibonacci(i));
}

//6.tree结构转换成list结构
const treeData = [
  {
    id: 1,
    pid: 0,
    name: "中国",
    children: [
      {
        id: 2,
        pid: 1,
        name: "北京",
        children: [
          { id: 3, pid: 2, name: "海淀" },
          { id: 4, pid: 2, name: "昌平" },
        ],
      },
      {
        id: 3,
        pid: 1,
        name: "河南",
        children: [
          { id: 5, pid: 3, name: "郑州" },
          { id: 6, pid: 3, name: "焦作" },
          { id: 7, pid: 3, name: "南阳" },
        ],
      },
    ],
  },
];

function treeToList(tree) {
  //处理后的list数组
  const treeTemp = [];
  const fn = (tree) => {
    tree.forEach((item) => {
      if (item.children && item.children.length > 0) {
        treeTemp.push(item);
        fn(item.children);
      } else {
        treeTemp.push(item);
      }
    });
  };

  fn(tree);

  return treeTemp.map((item) => {
    return {
      id: item.id,
      pid: item.pid,
      name: item.name,
    };
  });
}

//7.将list结构转换成tree结构
const list = [
  { id: 1, pid: 0, name: "中国" },
  { id: 2, pid: 1, name: "北京" },
  { id: 3, pid: 2, name: "海淀" },
  { id: 4, pid: 2, name: "昌平" },
  { id: 5, pid: 1, name: "河南" },
  { id: 6, pid: 5, name: "郑州" },
  { id: 7, pid: 5, name: "焦作" },
  { id: 8, pid: 5, name: "南阳" },
];

function listToTree(list, id, pid, children) {
  //深拷贝
  const cloneData = JSON.parse(JSON.stringify(list));

  const result = cloneData.filter((fatherItem) => {
    //找出有子级的数组
    const childTree = cloneData.filter((childItem) => {
      return childItem[pid] === fatherItem[id];
    });

    //如果子级不是空的，则将子级添加到父级的children上
    childTree.length > 0 ? (fatherItem[children] = childTree) : "";

    //pid===0时就返回
    return fatherItem[pid] === 0;
  });

  return result;
}

/**
 * list:代表要处理的list数组
 * id:代表唯一值
 * pid:父id
 * children:子级
 */
// const res1 = listToTree(list, "id", "pid", "children");
// console.log("list转换成tree的结果：", res1);

//8.检测是否是回文 例如：’abcba‘  abcdef

const str1 = "abcde";

function checkisHuiWen(str) {
  return str.split("").reverse().join("") === str;
}

console.log(checkisHuiWen(str1));

//9.判断一个对象是否为空
//算法1 利用JSON.stringify实现
const checkIsEmpty = (obj) => {
  return JSON.stringify(obj) === "{}";
};

//算法2
const checkIsEmpty2 = (obj) => {
  return Object.keys(obj).length === 0;
};

//算法3  obj.hasOwnProperty('自身属性')
const checkIsEmpty3 = (obj) => {
  let flag = true;
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      flag = false;
    }
  }

  return flag ? "是空的" : "不是空的";
};

let obj11 = {};
obj11.name = "jack";
//console.log(checkIsEmpty3(obj11));

//10.数组排序
//2.思路1：使用数组的sort排序
const arrs = [2, 235, 235, 253, 2, 12, 2, 23, 234, 24, 235];

//第一趟  2，235，235,2,12,2,23,234,24,235,253
//第二趟  2,235,2,12,2,23,234,24,235,235,253
//第三趟
//第四趟

// arrs.sort((v1, v2) => {
//   return v2 - v1;
// });
// console.log("排序后：", arrs);
//3.思路2：使用冒泡排序，双重循环，外循环负责趟数，内循环负责每趟比较的次数，然后两两进行比较，如果前者大于后者则交换
function sortBy(arrs) {
  for (let i = 0; i < arrs.length - 1; i++) {
    for (let j = 0; j < arrs.length - 1 - i; j++) {
      if (arrs[j] > arrs[j + 1]) {
        let temp = arrs[j];
        arrs[j] = arrs[j + 1];
        arrs[j + 1] = temp;
      }
    }
  }

  return arrs;
}

console.log("冒泡排序后的结果：", sortBy(arrs));

//3.需求:将url参数转换为对象，例如：`?ie=utf=8&f=8转换成{ie:'utf-8',f:8}`
function urlParamsToObj(url, key) {
  const resArr = url.split("&");
  let obj = {};
  resArr.forEach((item) => {
    let itemArr = item.split("=");
    obj[itemArr[0]] = itemArr[1];
  });

  return obj[key];
}

const url = location.search.slice(1);
console.log(urlParamsToObj(url, "fmq"));
